import {createRouter, createWebHashHistory} from 'vue-router';
import root from "@/views/root/home/root.vue";
import Home from "@/views/root/home/Home.vue";
import Patient from "@/views/root/Patient/Patient.vue";
import Escort from "@/views/root/Escort/Escort.vue";
import Appointment from "@/views/root/Appointment/Appointment.vue";
import Appointment2 from "@/views/root/Appointment/Appointment2.vue";
import ServiceRecord from "@/views/root/ServiceRecord/ServiceRecord.vue";
import User from "@/views/root/User/User.vue";
import Login from "@/views/Login/Login.vue";
import {ElMessage} from 'element-plus';
import Home2 from "@/views/customer/Home-customer.vue";
import Home1 from "@/views/escort/Home-escort.vue";
import Home0 from "@/views/all/Home0.vue";
import User0 from "@/views/root/User/User0.vue";
import User1 from "@/views/root/User/User1.vue";
import User2 from "@/views/root/User/User2.vue";
import Customer from "@/views/customer/login/Customer.vue";
import Rootpath from "@/views/all/root_path.vue";

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            component: Rootpath,
            redirect: '/login',
            children: [
                {path: 'home', component: Home, meta: {requiresAuth: false}},
                {path: 'login', component: Login, meta: {requiresAuth: false}},
                {path: 'home2', component: Home2, meta: {requiresAuth: false}},
                {
                    path: '/root/',
                    component: root,
                    redirect: '/root/home',
                    children: [
                        {path: 'home', component: Home, meta: {requiresAuth: false}},
                        {path: 'login', component: Login, meta: {requiresAuth: false}},
                        {path: 'patient', component: Patient, meta: {requiresAuth: true, permission: 'root'}},
                        {path: 'escort', component: Escort, meta: {requiresAuth: true, permission: 'root'}},
                        {path: 'appointment', component: Appointment, meta: {requiresAuth: true, permission: 'root'}},
                        {path: 'appointment2', component: Appointment2, meta: {requiresAuth: true, permission: 'customer'}},
                        {path: 'serviceRecord', component: ServiceRecord, meta: {requiresAuth: true, permission: 'root'}},
                        {path: 'user', component: User, meta: {requiresAuth: true, permission: 'root'}},
                        {path: 'user0', component: User0, meta: {requiresAuth: true, permission: 'root'}},
                        {path: 'user1', component: User1, meta: {requiresAuth: true, permission: 'root'}},
                        {path: 'user2', component: User2, meta: {requiresAuth: true, permission: 'root'}}
                    ]
                },
                {
                    path: '/escort/',
                    component: Home1,
                    redirect: '/escort/home1',
                    children: [
                        {path: 'home1', component: Home1, meta: {requiresAuth: false}},
                        {path: 'login', component: Login, meta: {requiresAuth: false}},
                        {path: 'patient', component: Patient, meta: {requiresAuth: true, permission: 'escort'}},
                        {path: 'escort', component: Escort, meta: {requiresAuth: true, permission: 'escort'}},
                    ]
                },
                {
                    path: '/customer/',
                    component: Customer,
                    redirect: '/customer/customer',
                    children: [
                        {path: 'customer', component: Customer, meta: {requiresAuth: false}},
                        // {path: 'home2', component: Home2, meta: {requiresAuth: false}},
                        {path: 'login', component: Login, meta: {requiresAuth: false}},
                        {path: 'patient', component: Patient, meta: {requiresAuth: true, permission: 'customer'}},
                        {path: 'appointment2', component: Appointment2, meta: {requiresAuth: true, permission: 'customer'}}
                    ]
                },
            ]
        },
        // 通配符路由，匹配所有未定义的路径
        {
            path: '/:pathMatch(.*)*',
            name: 'home0',
            component: Home0
        }
    ]
});

router.beforeEach((to, from, next) => {
    if (to.meta.requiresAuth) {
        const user = JSON.parse(localStorage.getItem('user'));
        if (!user) {
            ElMessage({message: '请先登录', type: 'error'});
            next('/login');
        } else {
            if (to.meta.permission && to.meta.permission !== user.user_permission) {
                ElMessage({message: '你没有权限访问该页面', type: 'error'});
                next(from.path);
            } else {
                next();
            }
        }
    } else {
        next();
    }
});

export default router;